home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / go / prog / nextgo23.taz / nextgo23 / NeXTGo / igsglue.m < prev    next >
Encoding:
Text File  |  1993-02-09  |  8.6 KB  |  417 lines

  1. #include "comment.header"
  2. #include "igs.h"
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "shared.h"
  7.  
  8. #import <appkit/appkit.h>
  9. #import "GoApp.h"
  10. #import "Board.h"
  11.  
  12. typedef piece boardtype[19][19];
  13.  
  14. message mesg;
  15.  
  16. char prefix[20];
  17. int boardon = 0;
  18. int boardmode = 0;
  19. int beepcount = 1;
  20.  
  21. int ingame = -1;
  22. extern int MAXX, MAXY;
  23.  
  24. struct {
  25.   char name[50];
  26.   char site[100];
  27.   int port;
  28. }  sitetable[10] =
  29. {
  30.   { "icsi", "icsib18.icsi.Berkeley.EDU", 6969},
  31.   {  "cnam", "cnam.cnam.fr", 6969 },
  32.   { "lacerta", "lacerta.unm.edu", 6969}, 
  33.   { "", "", 6969}, 
  34.   { "", "", 6969},
  35.   { "", "", 6969},
  36.   { "", "", 6969},
  37.   { "", "", 6969}, 
  38.   { "", "", 6969},
  39.   { "", "", 6969}
  40. };
  41.  
  42. char local[1000], *loc;
  43.  
  44. int observing = 0;
  45.  
  46. void showboard(boardtype b)
  47. {
  48.   extern unsigned char p[19][19];
  49.   int i, j;
  50.  
  51.   for (i = 0; i < 19; i++)
  52.     for (j = 0; j < 19; j++)
  53.       p[i][j] = b[i][j];
  54.  
  55.   [[NXApp getGoView] refreshIO];
  56. }
  57.  
  58. void igsbeep(void)
  59. {
  60.   id beepSound;
  61.  
  62.   beepSound = [Sound findSoundFor:"Bonk"];
  63.   [beepSound play];
  64. }
  65.  
  66. int startgame(int n)
  67. {
  68.   char str[100];
  69.   int ret;
  70.   
  71.   sprintf(str, "games %d\n", n);
  72.   sendstr(str);
  73.   do {
  74.     do {
  75.       ret = getmessage(&mesg, 0);
  76.       if (ret < 0)
  77.     exit(1);
  78.     } while (!ret);
  79.     if (mesg.id == MOVE)
  80.       [NXApp SetIGSStatus:"%Premature move.  Restart game.\n"];
  81.   }
  82.   while (mesg.id != GAMES);
  83.   if (mesg.gamecount != 1)
  84.     return -1;
  85.   if (mesg.gamelist[0].bsize > 19) {
  86.     [NXApp SetIGSStatus:"%Boardsize too large\n"];
  87.     return -1;
  88.   }
  89.   if (observing) {
  90.     [NXApp SetIGSStatus:"Removing observe\n"];
  91.     sprintf(str, "unobserve\n");
  92.     sendstr(str);
  93.     do {
  94.       do {
  95.     ret = getmessage(&mesg, 0);
  96.     if (ret < 0)
  97.       exit(1);
  98.       } while (!ret);
  99.     } while (mesg.id != PROMPT);
  100.     observing = 0;
  101.   }
  102.   ingame = n;
  103.   MAXX = MAXY = mesg.gamelist[0].bsize;
  104.   [[NXApp getGoView] startNewGame];
  105.   [[NXApp getGoView] display];
  106.   [[NXApp getGoView] setGameNumber:ingame];
  107.   sprintf(str, "%s (%s)", mesg.gamelist[0].white, mesg.gamelist[0].wrank);
  108.   [[NXApp getGoView] setWhiteName:str];
  109.   sprintf(str, "%s (%s)", mesg.gamelist[0].black, mesg.gamelist[0].brank);
  110.   [[NXApp getGoView] setBlackName:str];
  111.   [[NXApp getGoView] setIGSHandicap:mesg.gamelist[0].hcap];
  112.   sprintf(str, "%3.1f", mesg.gamelist[0].komi);
  113.   [[NXApp getGoView] setIGSKomi:str];
  114.   boardon = 1;
  115.   return 0;
  116. }
  117.  
  118. void makemove(int x, int y, int movenum, int color, int btime, int bbyo,
  119.           int wtime, int wbyo)
  120. {
  121.   extern void sethand(int);
  122.  
  123.   if ((x < MAXX) && (y < MAXY))
  124.     {
  125.       [[NXApp getGoView] makeMove: color: x: y];
  126.       [[NXApp getGoView] dispTime: btime: bbyo: wtime: wbyo];
  127.     }
  128.   else if (x > 100)
  129.     {
  130.       sethand(x-100);
  131.       [[NXApp getGoView] display];
  132.     }
  133. }
  134.  
  135. void makemovesilent(int x, int y, int movenum, int color, int btime, int bbyo,
  136.           int wtime, int wbyo)
  137. {
  138.   extern void sethand(int);
  139.  
  140.   if ((x < MAXX) && (y < MAXY))
  141.     {
  142.       [[NXApp getGoView] makeMoveSilent: color: x: y];
  143.     }
  144.   else if (x > 100)
  145.     {
  146.       sethand(x-100);
  147.     }
  148. }
  149.  
  150. void removeGroup(int x, int y)
  151. {
  152.   extern unsigned char p[19][19], patternmat[19][19];
  153.   extern int blackCaptured, whiteCaptured, currentStone;
  154.   extern void find_pattern_in_board(int,int);
  155.   int i, j;
  156.  
  157.   currentStone = p[x][y];
  158.  
  159.   find_pattern_in_board(x,y);
  160.   for (i = 0; i < MAXX; i++)
  161.     for (j = 0; j < MAXY; j++)
  162.       if (patternmat[i][j])
  163.     {
  164.       p[i][j] = EMPTY;
  165.       if (currentStone==BLACK)
  166.         blackCaptured++;
  167.       else
  168.         whiteCaptured++;
  169.     }
  170.  
  171.   [[NXApp getGoView] setblacksPrisoners:blackCaptured];
  172.   [[NXApp getGoView] setwhitesPrisoners:whiteCaptured];
  173.  
  174.   [[NXApp getGoView] refreshIO];
  175. }
  176.  
  177. void getmoves(int n)
  178. {
  179.   int ret;
  180.   char str[100];
  181.   
  182.   sprintf(str, "moves %d\n", n);
  183.   sendstr(str);
  184.   do {
  185.     do {
  186.       ret = getmessage(&mesg, 0);
  187.       if (ret < 0)
  188.     exit(1);
  189.     } while (!ret);
  190.     if (mesg.id == MOVE)
  191.       makemovesilent(mesg.x, mesg.y, mesg.movenum, mesg.color, mesg.btime,
  192.            mesg.bbyo, mesg.wtime, mesg.wbyo);
  193.     else if (mesg.id && mesg.id != PROMPT)
  194.       [NXApp SetIGSStatus:mesg.text];
  195.   } while (mesg.id != PROMPT);    /* MOVE || mesg.id == 0); */
  196.   lastMove--;
  197.   makemove(mesg.x, mesg.y, mesg.movenum, mesg.color, mesg.btime,
  198.        mesg.bbyo, mesg.wtime, mesg.wbyo);
  199.   [[NXApp getGoView] refreshIO];
  200.   [[NXApp getGoView] display];
  201. }
  202.  
  203. void getgames(message *mess)
  204. {
  205.   int ret;
  206.  
  207.   sendstr("games\n");
  208.   do {
  209.     do {
  210.       ret = getmessage(mess, 0);
  211.       if (ret < 0)
  212.     exit(1);
  213.     } while (!ret);
  214.     if (mess->id == MOVE)
  215.       [NXApp SetIGSStatus:"%Premature move.  Restart game.\n"];
  216.   } while (mess->id != GAMES);
  217. }
  218.  
  219. void unobserve(void)
  220. {
  221.   char str[100];
  222.   sprintf(str, "unobserve %d\n", ingame);
  223.   sendstr(str);
  224.   /* observing=0; ingame= -1; setgame(-1); */
  225. }
  226.  
  227. int observegame(int n)
  228. {
  229.   int ret;
  230.   char str[20];
  231.   
  232.   if (!observing && ingame != -1) {
  233.     [NXApp SetIGSStatus:"Can't observe while playing.\n"];
  234.     return 1;
  235.   }
  236.   if (startgame(n))
  237.     return 1;
  238.   getmoves(n);
  239.   sprintf(str, "observe %d\n", n);
  240.   sendstr(str);
  241.   observing = 1;
  242.   do {
  243.     do {
  244.       ret = getmessage(&mesg, 0);
  245.       if (ret < 0)
  246.     exit(1);
  247.     } while (!ret);
  248.     if ((mesg.id == INFO) && !strncmp(mesg.text, "Removing", 8))
  249.       [NXApp SetIGSStatus:"%fatal sync error.  Restart igs.\n"];
  250.   } while (mesg.id != MOVE && mesg.id != UNDO);
  251.   return 0;
  252. }
  253.  
  254.  
  255. int peekgame(int n)
  256. {
  257.   if (!observing && ingame != -1) {
  258.     [NXApp SetIGSStatus:"Can't peek while playing.\n"];
  259.     return 1;
  260.   }
  261.   if (startgame(n))
  262.     return 1;
  263.   getmoves(n);
  264.   setgame(-1);
  265.   return 0;
  266. }
  267.  
  268. void setgame(int newgame)
  269. {
  270.   if (newgame != ingame) {
  271.     ingame = newgame;
  272.     [[NXApp getGoView] setGameNumber:ingame];
  273.   }
  274. }
  275.  
  276. void loadgame(char *name)
  277. {
  278.   char str[100];
  279.   int ret;
  280.   sprintf(str, "load %s\n", name);
  281.   sendstr(str);
  282.   do {
  283.     ret = getmessage(&mesg, 0);
  284.     if (ret < 0)
  285.       exit(1);
  286.     sprintf(str, "&&%d&&\n", mesg.id);
  287.     [NXApp SetIGSStatus:str];
  288.   } while (mesg.id != MOVE && mesg.id != ERROR);
  289.   if (mesg.id == ERROR)
  290.     [NXApp SetIGSStatus:mesg.text];
  291.   else {
  292.     if (!startgame(mesg.gamenum))
  293.       getmoves(mesg.gamenum);
  294.   }
  295. }
  296.  
  297. void doserver(void)
  298. {
  299.   int ret;
  300.   NXEvent peek_ev, *get_ev;
  301.   
  302.   loc = local;
  303.   idle = 0;
  304.   ret = getmessage(&mesg, 1);
  305.   if (ret < 0 && ret != KEY) {
  306.     [NXApp SetIGSStatus:"Connection closed\n"];
  307.   }
  308.   if (ret > 0)
  309.     switch (mesg.id) {
  310.     case QUITMESG:
  311.       [NXApp SetIGSStatus:mesg.text];
  312.       break;
  313.     case ONSERVER:
  314.       [NXApp SetIGSStatus:"Connection established\n"];
  315.       break;
  316.     case BEEP:
  317.       break;
  318.     case MOVE:
  319.       if (!boardon)
  320.     [NXApp SetIGSStatus:"%Error: isolated move received\n"];
  321.       else {
  322.     makemove(mesg.x, mesg.y, mesg.movenum, mesg.color, mesg.btime,
  323.          mesg.bbyo, mesg.wtime, mesg.wbyo);
  324.     setgame(mesg.gamenum);
  325.       }
  326.       break;
  327.     case UNDO:
  328.       if (!boardon)
  329.     [NXApp SetIGSStatus:"%Error: isolated undo received"];
  330.       else {
  331.     setgame(mesg.gamenum);
  332.     [[NXApp getGoView] undo];
  333.     [[NXApp getGoView] display];
  334.       }
  335.       break;
  336.     case SCOREUNDO:
  337.       /*    endgame();  */
  338.       [NXApp SetIGSStatus:"Scoring undone."];
  339.       break;
  340.     case LOAD:
  341.       if (!startgame(mesg.gamenum))
  342.     getmoves(mesg.gamenum);
  343.       break;
  344.     case MATCH:
  345.       startgame(mesg.gamenum);
  346.       break;
  347.     case REMOVE:
  348.       removeGroup(mesg.x, mesg.y);
  349.       break;
  350.     case SCORE:
  351.       {
  352.     char str[50];
  353.     showboard(mesg.board);
  354.     sprintf(str, "Black: %g\nWhite: %g\n", mesg.bscore, mesg.wscore);
  355.     [NXApp SetIGSStatus:str];
  356.       }
  357.       break;
  358.     case LOOK_M:{
  359.       int pris[2];
  360.       if (mesg.boardsize > 19)
  361.     [NXApp SetIGSStatus:"%Boardsize of saved game too big.\n"];
  362.       else {
  363.     MAXX = MAXY = mesg.boardsize;
  364.     [[NXApp getGoView] startNewGame];
  365.     [[NXApp getGoView] display];
  366.     boardon = 1;
  367.     pris[0] = mesg.bcap;
  368.     pris[1] = mesg.wcap;
  369.     [[NXApp getGoView] setblacksPrisoners:pris[0]];
  370.     [[NXApp getGoView] setwhitesPrisoners:pris[1]];
  371.     [[NXApp getGoView] refreshIO];
  372.     showboard(mesg.board);
  373.       }
  374.     }
  375.       break;
  376.     case KIBITZ:{
  377.       char s[300];
  378.       sprintf(s, "%s: %s\n", mesg.kibitzer, mesg.kibitz);
  379.       [NXApp SetIGSStatus:s];
  380.     }
  381.       break;
  382.     case STORED:
  383.       if (!strlen(mesg.text))
  384.     [NXApp SetIGSStatus:"No stored games\n"];
  385.       else
  386.     [NXApp SetIGSStatus:mesg.text];
  387.       break;
  388.     case INFO:
  389.       if (strstr(mesg.text, "Removing")) {
  390.     observing = 0;
  391.     setgame(-1);
  392.     [NXApp SetIGSStatus:mesg.text];
  393.       } else
  394.     [NXApp SetIGSStatus:mesg.text];
  395.       break;
  396.     case PROMPT:
  397.       if (ingame != -1 && mesg.prompttype == 5) {
  398.     setgame(-1);
  399.     observing = 0;
  400.       }
  401.     case 0:
  402.       break;
  403.     default:
  404.       [NXApp SetIGSStatus:mesg.text];
  405.       break;
  406.     }
  407.   idle = 1;
  408.       
  409.   if( [NXApp peekNextEvent: NX_MOUSEDOWNMASK into: &peek_ev] )
  410.     {
  411.       get_ev = [NXApp getNextEvent: NX_MOUSEDOWNMASK];
  412.       [NXApp sendEvent: get_ev];
  413.     }
  414. }
  415.  
  416.  
  417.